package jmu.cultural_tourism.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jmu.cultural_tourism.entity.SysUserinfo;
import jmu.cultural_tourism.service.SysUserinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


/*用户表(SysUserinfo)表控制层*/
@RestController
@Tag(name="用户接口")
@RequestMapping("sysUserinfo")
public class SysUserinfoController {
    /*服务对象*/
    @Autowired
    private SysUserinfoService sysUserinfoService;


    @PostMapping("login")
    @Operation(summary = "用户登录")
    public ResponseEntity<SysUserinfo> login(
            @RequestParam String account,
            @RequestParam String password
    ) {
        SysUserinfo sysUserinfo = sysUserinfoService.queryByAccount(account);
        if (sysUserinfo == null || !sysUserinfo.getPassword().equals(password)) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
        }
        return ResponseEntity.ok(sysUserinfo);
    }
    /**
     * 分页查询
     *
     * @param sysUserinfo 筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<SysUserinfo>> queryByPage(SysUserinfo sysUserinfo, PageRequest pageRequest) {
        return ResponseEntity.ok(this.sysUserinfoService.queryByPage(sysUserinfo, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<SysUserinfo> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.sysUserinfoService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param sysUserinfo 实体
     * @return 新增结果
     */
    @PostMapping("addUser")
    @Operation(summary = "用户注册")
    public ResponseEntity<SysUserinfo> add(@RequestBody SysUserinfo sysUserinfo) {
        return ResponseEntity.ok(this.sysUserinfoService.insert(sysUserinfo));
    }

    /*编辑数据*/
    @PutMapping
    public ResponseEntity<SysUserinfo> edit(SysUserinfo sysUserinfo) {
        return ResponseEntity.ok(this.sysUserinfoService.update(sysUserinfo));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.sysUserinfoService.deleteById(id));
    }

}

